﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyPlant : MonoBehaviour {
    public static EnemyPlant con;

    public void Awake()
    {
        con = this;
    }
    public float speed;
    public LayerMask layer;
    
    public Transform Cpoint;
    public Transform bulletPre;
    public Transform Plantpot;

    private bool Collid;

    private Rigidbody2D rg;
    private CapsuleCollider2D ca;
    private Animator an;

    public bool isShot;
    // Use this for initialization
    void Start()
    {
        rg = GetComponent<Rigidbody2D>();
        ca = GetComponent<CapsuleCollider2D>();
        an = GetComponent<Animator>();
    }

    private void FixedUpdate()
    {
        Collid = Physics2D.OverlapCircle(Cpoint.position, 40, layer);

        if (Collid)
        {
            an.SetBool("IsAttack", true);
            if (!isShot)
            {
                isShot = true;
                InvokeRepeating("Shot", 1.2f, 2.0f);
            }
        }
        else
        {
            isShot = false;
            an.SetBool("IsAttack", false);
            CancelInvoke("Shot");
        }
    }

    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            an.SetTrigger("IsHit");
            ca.enabled = false;
            rg.simulated = false;
            Destroy(gameObject, 0.3f);
            PlayerBloodController.con.SetBlood();
            PlayerController.con.PlayerHurt();
        }
        if (other.gameObject.tag=="sword")
        {
            an.SetTrigger("IsHit");
            ca.enabled = false;
            rg.simulated = false;
            Destroy(gameObject, 0.3f);
            CoinController.con.CreatCoin(Cpoint, 10);
        }
    }

    public void Shot()
    {
        Instantiate(bulletPre, Cpoint.transform.position, Cpoint.transform.rotation, Plantpot);
    }
}
